import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";
import compress from "gql-compress";

const usePosition = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
        positions: [],
    });

    const loadPositions = async () => {
        state.loading = true;
        let res = await graphqlFetch({
            query: allOrganizationPositions,
            variables: {},
        });
        state.positions = res?.a?.nodes || [];
        state.loading = false;
    };

    useEffect(() => {
        loadPositions();
    }, []);

    return { ...state, loadPositions };
};

const allOrganizationPositions = compress(`query MyQuery {
    a: allOrganizationPositions(orderBy: PY_NAME_ASC) {
        totalCount
        nodes {
            opUuid
            opStat
            opName
            opDesc
        }
    }
}`);

export default usePosition;
